import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;

//
// 数据流,弃用表达式
public class ParamControl {

    public static Collection<?> removable(Collection<?> collection){
        return filter(collection,item->{
            //留下不是null或空字符串的参数
            //没带的参数天然就不存在
            return !(item==null||item.toString().equals(""));
        });
    }

    public static <T> Collection<? extends T> filter(Collection<T> collection, Predicate<? super T> predicate){
        assert (collection!=null);
        return collection.stream().filter(predicate).toList();
    }

    public static <K> void applyFixedParam(Consumer<Object> wp){

    }
    public static void applyOptionalParam(){

    }

    private static final class Qwrapper{
        public void eq(String s){
            System.out.println("eq()");
            System.out.println("    >参数: "+s);
        }
        public void apply(String s){
            System.out.println("apply()");
        }
        public void in(String s){
            System.out.println("in()");
        }
        public void and(String s){
            System.out.println("and()");
        }
        public  void clear(){
            System.out.println("clear()");
        }
    }

    private static void wrap_qwr(Qwrapper qw){

    }
    private enum GType{
        String_T("String"),
        Long_T("Long or long"),
        List_T("List<?>"),
        Object_T("Object");
        private String tname;
        GType(String name){
            tname=name;
        }
    }
    private static final class Pair<T1>{
        public GType type;
        public T1 value;
        public Pair(GType gt,T1 v){
            type=gt;
            value=v;
        }

    }

    private static final class MaybeApply<T>{
        private Map<String,Object> map;
        private static final Optional<?> empty=Optional.empty();
        private  Map<String,Pair<Optional<Object>>> cache=new HashMap<>();
        private  Map<String,Predicate<T>> check=new HashMap<>();

        public MaybeApply(Map<String,Object> d){
            map=d;
        }

        public Optional<?> getp(String p){
            Pair<Optional<Object>> holder = cache.get(p);
            if(holder!=null)
                return holder.value;
            Object o = map.get(p);
            //默认检查
            if(o==null||o.toString().equals("")){
                return empty;
            }else{
//                if(check.get(p).test(o))
                Optional<Object> o1 = Optional.of(o);
                cache.put(p,new Pair<>(GType.Object_T,o1));
                return o1;
//                return empty;
            }
        }

        public void addCheck(String p,Predicate<T> c){
            check.put(p,c);
        }

//        public Qwrapper getp2(String p){
//            Object o = map.get(p);
//            if(o==null||o.toString().equals("")){
//                return oldp.get();
//            }else{
//                return pp;
//            }
//        }

    }

    public static final Set<String> null_check=new HashSet<>();
//    public static final Set<String> null_check=new HashSet<>();
//    public static final Set<String> null_check=new HashSet<>();

    private static final class ParamHolder<T> {
        public T value;
        public boolean discard = false;

        public ParamHolder(T v, boolean b) {
            discard = b;
            value=v;
        }

        public void maatry(Consumer<T> s) {
            if (!discard) {
                s.accept(value);
            }
        }
    }


    private static final class ParamWrap{
        public Map<String,Object> pp=null;
        public ParamWrap(Map<String,Object> map){
            pp=map;
        }
        public Object get(String key){
            if(tryApply(key))
                return pp.get(key);
            throw new RuntimeException("参数无法通过检验");
        }
        public boolean tryApply(String k){
            Object o = pp.get(k);
            if(o!=null&&o.toString().equals(""))
                return true;
            return false;
        }

    }

    public static List<String> bindParam(String p_list){
        return null;
    }
    @Test
    public void testMapNull(){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("a",null);
        hashMap.put("b",6);
        hashMap.put("c","hekko");
        hashMap.put("d",5.3);
        hashMap.put("dx","");
        Qwrapper qwrapper = new Qwrapper();
        MaybeApply maybeApply = new MaybeApply(hashMap);
        Optional<Object> d = maybeApply.getp("d");
        maybeApply.getp("a").ifPresent(p->qwrapper.eq(p.toString()));
        maybeApply.getp("b").ifPresent(p->qwrapper.eq(p.toString()));
        maybeApply.getp("d").ifPresent(p->qwrapper.eq(p.toString()));
        maybeApply.getp("dx").ifPresent(p->qwrapper.eq(p.toString()));
    }
}
